package controllers

import (
	"net/http"
	httpEx "git.oschina.net/Ljohn/Base/lib/http"
	errEx "git.oschina.net/Ljohn/Base/lib/error"
	resEx "git.oschina.net/Ljohn/Base/model/result"
	modelTest "git.oschina.net/Ljohn/Base/model/test"
	sys "git.oschina.net/Ljohn/Base/sys/test"
	"encoding/json"
)


func Test(w http.ResponseWriter, req *http.Request) {
	nameArg,err := httpEx.GetRawQueryByArgs(req.URL.RawQuery,"name")
	if err !=nil || nameArg ==""{
		errEx.HandleError(err)
		w.Write(resEx.GetErrorResult("参数未找到"))
		return
	}
	ageArg,err := httpEx.GetRawQueryByArgs(req.URL.RawQuery,"age")
	if err !=nil || ageArg ==""{
		errEx.HandleError(err)
		w.Write(resEx.GetErrorResult("参数未找到"))
		return
	}
	resByte,err :=sys.GetModel(nameArg,ageArg)
	if err !=nil{
		errEx.HandleError(err)
		w.Write(resEx.GetErrorResult("查询错误"))
		return
	}
	modelTemp :=  &modelTest.TestModel{}

	err= json.Unmarshal(resByte,modelTemp)
	if err !=nil{
		errEx.HandleError(err)
		w.Write(resEx.GetErrorResult("转换JSON失败"))
		return
	}
	resModel,_ := resEx.SetResult(200,"succ",modelTemp)
	w.Write(resModel)
}
